﻿package smooth.core.object.interactiveTextComponent {
	
	import app.core.action.RotatableScalable;
	import flash.geom.Point;
	import smooth.core.object.flickrComponent.FlickrComponent;
	import smooth.core.object.youTubeComponent.YouTubeComponent;
	import smooth.core.object.googleMapComponent.GoogleMapComponent;
	import smooth.core.object.googleMapCanvasComponent.GoogleMapCanvasComponent;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.AntiAliasType;
	
	import caurina.transitions.Tweener;	
	import app.core.element.Wrapper;
	
	public class InteractiveTextComponent extends RotatableScalable
	{
		[Embed(source="Arial.ttf", fontFamily="myFont")] 
		public var myFont:Class;
		
		public var t: TextField;
		public var mc: Sprite;
		public var inString:String;	
		public var closeButton:Boolean;	
			
		public function InteractiveTextComponent(inVar:String, _closeButton:Boolean) {	
			// Init des attributs de l'objet
			inString = inVar;
			closeButton	= _closeButton;	
			// Init des visuels du composant			
			initGraphics();
			// Init des events du composant
			initEvents();	
			// Init des process du composant
			initProcess();
		}   
		
		private function initGraphics():void {
			var noSelection:Boolean = true;			
			var format:TextFormat= new TextFormat();
			format.font= "myFont";
			format.color= 0xFFFFFF;
			format.size= 72;
			
			t = new TextField();
			t.background = true;	
			t.backgroundColor = 0x000000;	
			t.border = true;	
			t.borderColor = 0x333333;			
			t.embedFonts = true;
			t.selectable  = true;		 
			if (inString != ' ') {
				t.text = ' ' + inString + ' ';
			} else {
				t.text = "~";
			}
			t.autoSize = TextFieldAutoSize.CENTER;
			t.name = "t";
			t.x = 0;
			t.y = -36;
			t.setTextFormat(format);
			this.addChild(t);	    
						
			if(closeButton){
				var buttonSprite:Sprite = new Sprite();						
				buttonSprite.graphics.beginFill(0xFFFFFF,0.75);
				buttonSprite.graphics.lineStyle(1,0x000000,0.85);
				buttonSprite.graphics.drawRoundRect( -t.width / 2, -63, t.width + 4, 25, 6);
				// TODO Bouger la ligne suivante dans InitEvents()
				buttonSprite.addEventListener(MouseEvent.CLICK, this.doDisappear);	
				var WrapperObject:Wrapper = new Wrapper(buttonSprite);					
				this.addChild(WrapperObject);
			}
		}
		
		private function initProcess():void {
		}
		
		private function initEvents():void {
			this.addEventListener(TouchEvent.MOUSE_UP, this.doTouchUp);
			this.addEventListener(MouseEvent.MOUSE_UP, this.doMouseUp);
		}
		
		private function doDisappear(e:Event):void {
			Tweener.addTween(this, {alpha:0,scaleX: 0.0, scaleY: 0.0, time:0.25, transition:"easeinoutquad"});	
		}
		
		private function doMouseUp(e:MouseEvent):void {
			var point:Point = new Point(e.stageX, e.stageY);
			doProcessMouseUp(point);
		}
		
		private function doTouchUp(e:TouchEvent):void {
			var point:Point = new Point(e.stageX, e.stageY);
			doProcessMouseUp(point);
		}
  
		private function doProcessMouseUp(point:Point):void {
			var actionsToProcess:Array = new Array();
			var targetObjects:Array = this.stage.getObjectsUnderPoint(point);
			actionsToProcess.unshift({objectType:"", targetObject:null})
			for (var i:int = 0; i < targetObjects.length;++i) {
				var targetObject:Object = targetObjects[i];
				if (targetObject is FlickrComponent) {
					actionsToProcess.unshift({objectType:"FlickrComponent", targetObject:targetObject});
				} else if (targetObject is YouTubeComponent) {
					actionsToProcess.unshift({objectType:"YouTubeComponent", targetObject:targetObject});
				} else if (targetObject is GoogleMapCanvasComponent) {
					actionsToProcess.unshift({objectType:"GoogleMapCanvasComponent", targetObject:targetObject});
				} else if (targetObject is GoogleMapComponent) {
					actionsToProcess.unshift({objectType:"GoogleMapComponent", targetObject:targetObject});
				}
			}
			
			if (actionsToProcess[0].objectType == "FlickrComponent") {
				actionsToProcess[0].targetObject.textSearchService(this);
				Tweener.addTween(this, {alpha:0,scaleX: 0.0, scaleY: 0.0, time:0.25, transition:"easeinoutquad"});	
			} else if (actionsToProcess[0].objectType == "YouTubeComponent") {
				actionsToProcess[0].targetObject.textSearchService(this);
				Tweener.addTween(this, {alpha:0,scaleX: 0.0, scaleY: 0.0, time:0.25, transition:"easeinoutquad"});	
			} else if (actionsToProcess[0].objectType == "GoogleMapCanvasComponent") {
				actionsToProcess[0].targetObject.geocodeService(this);
				Tweener.addTween(this, {alpha:0,scaleX: 0.0, scaleY: 0.0, time:0.25, transition:"easeinoutquad"});	
			} else if (actionsToProcess[0].objectType == "GoogleMapComponent") {
				actionsToProcess[0].targetObject.geocodeService(this);
				Tweener.addTween(this, {alpha:0,scaleX: 0.0, scaleY: 0.0, time:0.25, transition:"easeinoutquad"});	
			}
			
		}
		
		
		public function getText():String {
			return this.t.text;
		}
	}
	
}